import { getBaseUrl } from '@/services/urlconst';
import { getLoadMapList, getSelectDataMapList, getTenantID, requestFunComponFiex } from '@/utils';
import { useCallback, useMemo } from 'react';
import { useRequest } from 'umi';
import { MyAssetListManageAPI } from '../common/type';
import { ManageSearchColumns } from '../mconfig';
import { MyAssetListManageTab } from '../mconfig/ManageTabSwitch';
import { ILoadMapTypeAll } from '@/common';

let baseUrl = getBaseUrl();
// let baseUrl = '/wenchuang'; // 本地环境
const assetListManageServices = (curRes: Record<string, any>) => {
  /***获取数据字典**/
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_TYPE}`,
    `${getTenantID()}${ILoadMapTypeAll.ASSET_STATUS}`,
    `${getTenantID()}${ILoadMapTypeAll.ASSET_EQUITY}`,
  ];

  const loadMdicMapObj: Record<string, any> = getLoadMapList(loadMdicIds);

  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = MyAssetListManageTab(curRes);
  /**
   * 获取列表数据
   */
  const { tableProps, data, error, loading, run, params } = useRequest(
    (opt: MyAssetListManageAPI.IAssetTableRequestType) => {
      let personUrl = `${baseUrl}/assetInfo/list/${queryTypeObj?.params}`;
      let personBody: any = {
        ...queryTypeObj,
        order: 'desc',
        page: 1,
        rows: 10,
        sort: 'createTime',
        dicTypeIds: loadMdicIds?.toString(), // 字典入参
        assetState: `${getTenantID()}${ILoadMapTypeAll.ASSET_STATUS}1000`, // 查询在用
        ...opt,
      };
      return requestFunComponFiex('POST', personUrl, personBody);
    },
    {
      manual: true, //页面初始请求为true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getAssetListTableData = useCallback((otherRequest: MyAssetListManageAPI.IAssetTableRequestType | any) => {
    const request: MyAssetListManageAPI.IAssetTableRequestType | any = {
      ...otherRequest,
    };
    if (request?.useTime) {
      const [useTimeStart, useTimeEnd] = request.useTime;
      request['useTimeStart'] = useTimeStart.format('YYYY-MM-DD 00:00:00');
      request['useTimeEnd'] = useTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete request['useTime'];
    }
    delete request['useDepartmentFormNameDept'];
    delete request['useDepartmentFormNameUser'];
    if (request['userID']?.length) {
      request['userID'] = request['userID']?.toString();
    } else {
      delete request['userID'];
    }
    // if (request?.assetValue) {
    //   const [assetValueStart, assetValueEnd] = request.assetValue;
    //   request['assetValueMin'] = assetValueStart;
    //   request['assetValueMax'] = assetValueEnd;
    //   delete request['assetValue'];
    // }

    run({
      ...request,
    });
  }, []);

  /** 获取搜索参数 */
  const searchListParams = ManageSearchColumns(loadMdicMapObj, queryTypeObj);

  /* 页面列表 */
  let tableValues = useMemo(() => {
    return {
      assetListData: data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /***搜索功能配置columns**/
  const searchList = useMemo(() => {
    return {
      searchList: searchListParams,
    };
  }, [loadMdicMapObj?.loadMapInfo]);

  /* 数据字典值  */
  let loadMapList = useMemo(() => {
    return {
      loadMdicMapList: [],
    };
  }, [params]);

  return {
    loading,
    getAssetListTableData, //其他请求操作方法例如刷新等等
    // manageTableParams,
    otherParams: params?.[0],
    ...tableValues, //页面列表值
    ...loadMapList,
    ...searchList,
  };
};

/** 获取子资产数据 */
const assetListChildManageServices = (callback: (res: any) => void) => {
  /**
   * 获取列表数据
   */
  const { tableProps, data, error, loading, run, params } = useRequest(
    (opt: MyAssetListManageAPI.IAssetTableRequestType) => {
      let personUrl = `${baseUrl}/assetInfo/assetChild/${opt.id}`;
      let personBody: any = {
        order: 'desc',
        page: 1,
        rows: 10,
        sort: 'createTime',
        // dicTypeIds: loadMdicIds?.toString(), // 字典入参
        // ...opt,
      };
      return requestFunComponFiex('GET', personUrl, personBody);
    },
    {
      manual: true, //页面初始请求为true
      paginated: true,
      defaultPageSize: 10,
      onSuccess: callback,
    },
  );

  /* 获取页面数据 */
  const getAssetListChildTableData = useCallback((otherRequest: MyAssetListManageAPI.IAssetTableRequestType | any) => {
    const request: MyAssetListManageAPI.IAssetTableRequestType | any = {
      ...otherRequest,
    };

    run({
      ...request,
    });
  }, []);

  /* 页面列表 */
  let tableValues = useMemo(() => {
    return {
      assetListData: data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    getAssetListChildTableData, //其他请求操作方法例如刷新等等
    // manageTableParams, // 当前搜索参数
    otherParams: params?.[0],
    ...tableValues, //页面列表值
  };
};

export { assetListManageServices, assetListChildManageServices };
